package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.client.JunkTypeClient;
import com.woniu.client.dto.JunkTypeDto;
import com.woniu.exception.RebornException;
import com.woniu.mapper.StationMapper;
import com.woniu.mapper.StationRefuseJunkMapper;
import com.woniu.model.Result;
import com.woniu.model.Station;
import com.woniu.model.StationRefuseJunk;
import com.woniu.service.StationRefuseJunkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.service.param.AddStationParam;
import com.woniu.service.param.AddStationRefuseJunkParam;
import com.woniu.service.param.DelStationRefuseJunkParam;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author eason
 * @since 2024年05月25日
 */
@Service
public class StationRefuseJunkServiceImpl extends ServiceImpl<StationRefuseJunkMapper, StationRefuseJunk> implements StationRefuseJunkService {

    @Resource
    private StationRefuseJunkMapper stationRefuseJunkMapper;
    @Resource
    private StationMapper stationMapper;
    @Resource
    private JunkTypeClient junkTypeClient;

    /**
     * 站点添加垃圾黑名单
     * id: 站点id
     * typeId:垃圾种类id
     */
    @Override
    @GlobalTransactional
    public void add(AddStationRefuseJunkParam param) {
        Integer id = param.getId();
        Integer typeId = param.getTypeId();
        Station station = stationMapper.selectById(id);
        if (station == null) {
            throw new RuntimeException("不存在的站点");
        }
        Result<JunkTypeDto> byId = junkTypeClient.getById(typeId);
        if (byId.getCode() != 200) {
            throw new RuntimeException("不存在的垃圾种类");
        }
        //查出所有该站点的黑名单
        List<StationRefuseJunk> stationRefuseJunk = stationRefuseJunkMapper.selectList(new QueryWrapper<StationRefuseJunk>().eq("station_id", station.getId()));
        if (stationRefuseJunk != null) {
            //不为空(站点存在黑名单)的情况下,判断选择的类型是否已存在
            for (StationRefuseJunk refuseJunk : stationRefuseJunk) {
                if (refuseJunk.getRefuseJunkId().equals(typeId)) {
                    throw new RebornException("该垃圾类型已在黑名单中", 3001);
                }
            }
        }
        //新增
        stationRefuseJunkMapper.insert(new StationRefuseJunk().setRefuseJunkId(typeId).setStationId(id));
    }

    /**
     * 站点删除垃圾黑名单
     * id: 站点id
     * typeId:垃圾种类id
     */
    @Override
    public void del(DelStationRefuseJunkParam param) {
        Integer id = param.getId();
        Integer typeId = param.getTypeId();
        Station station = stationMapper.selectById(id);
        if (station == null) {
            throw new RuntimeException("不存在的站点");
        }
        Result<JunkTypeDto> byId = junkTypeClient.getById(typeId);
        if (byId.getCode() != 200) {
            throw new RuntimeException("不存在的垃圾种类");
        }
        //查出所有该站点的黑名单
        List<StationRefuseJunk> stationRefuseJunk = stationRefuseJunkMapper.selectList(new QueryWrapper<StationRefuseJunk>().eq("station_id", station.getId()));
        if (stationRefuseJunk == null) {
            throw new RebornException("该站点没有任何黑名单", 3002);
        }
        for (StationRefuseJunk refuseJunk : stationRefuseJunk) {
            if (refuseJunk.getRefuseJunkId().equals(typeId)) {
                stationRefuseJunkMapper.delete(new QueryWrapper<StationRefuseJunk>().eq("station_id",id).eq("refuse_junk_id",typeId));
                return;
            }
        }
        throw new RebornException("该站点的黑名单中没有选中的类型", 3003);
    }

    /**
     * 根据站点id查看该站点黑名单列表
     * id: 站点id
     */
    @Override
    public List<StationRefuseJunk> loadByStationId(Integer id) {
        Station station = stationMapper.selectById(id);
        if (station == null) {
            throw new RuntimeException("不存在的站点");
        }
        QueryWrapper<StationRefuseJunk> wrapper = new QueryWrapper<StationRefuseJunk>().eq("station_id", id);

        List<StationRefuseJunk> stationRefuseJunks = stationRefuseJunkMapper.selectList(wrapper);
        if (stationRefuseJunks==null){
            throw new RebornException("该站点没有黑名单",20005);
        }
        return stationRefuseJunks;
    }
}
